/**
 * 
 */
package cn.bblocks.cacheTest.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.repository.query.Param;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import cn.bblocks.cacheTest.model.User;
import cn.bblocks.cacheTest.model.dao.UserMapper;
import cn.bblocks.cacheTest.service.UserBankService;
import cn.bblocks.cacheTest.service.UserService;
import cn.bblocks.cacheTest.suport.RandomValue;
import cn.hutool.core.util.RandomUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * @author Administrator
 *
 */
@Data
@Slf4j
@Service
@CacheConfig(cacheNames = "User") 
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserMapper userMapper;
	@Autowired
	UserBankService userBankService;
	
	/* (non-Javadoc)
	 * @see com.wenkor.ticket.service.UserService#selectByLoginName(java.lang.String)
	 */
	@Cacheable( key = "'&loginName='+#p0")
	@Override
	public User selectByLoginName(String loginName) {
		// TODO Auto-generated method stub
		return userMapper.findByLoginName(loginName);
	}

	/* (non-Javadoc)
	 * @see com.wenkor.ticket.service.UserService#getUser(java.lang.Integer)
	 */
	@Override
	@Cacheable(key = "#id")  
	public User getUser(Integer id) {
		// TODO Auto-generated method stub
		return userMapper.findById(id).orElse(null);
	}
	
	
	public void addUser(User user) {
		userMapper.saveAndFlush(user);
	}
	
	public void addUsers(List<User> users) {
		userMapper.saveAll(users);
	}
	
	public List<User> randomUsers(){
		return userMapper.getRandomUsers(10);
	}
	
	
	

	 
	@Async
	 public void initUser(int i) {
		long b = System.currentTimeMillis();
		// log.info("ThreadName:====one====" + Thread.currentThread().getName());
		 User user = new User();
		 user.setLoginName("zhaojin_"+(i+1));
		 user.setLoginName(RandomValue.getTel());
		 user.setEmail(user.getLoginName()+"@qq.com");
		 
		 user.setEmail(RandomValue.getEmail(4, 10));
		 
		 user.setName("赵金"+(i+1));
		 user.setName(RandomValue.getChineseName());
		 
		 user.setSexy(RandomUtil.randomInt(1));
		 user.setStatus(1);
		 addUser(user);
		 
//		 if(users.size()>=2) {
//			 
//			 userService.addUsers(users); 
//			 users.clear();
//		 }
		 
		 userBankService.initUserBanks(user);
		 
		 long after = System.currentTimeMillis();
		 long be = (after - b)/1000;
		 if(be>10) {
			 log.info("耗时：" + be);
		 }
	 }
	
	public Long totalUsers() {
		return userMapper.count();
	}
	
	
	public List<User> query(String loginName
			,String name
			,Integer status){
		return userMapper.queryBy(loginName, name, status);
//		return null;
	}
}
